from flask import Flask, request,render_template
from datetime import datetime
# __name__: 当前 app.py这个模块
app = Flask(__name__)

class User:
    def __init__(self,username,email,password):
        self.username = username
        self.email = email


def datetime_format(value, expect_format="%Y-%m-%d %H:%M"):
    return value.strftime(expect_format)

app.add_template_filter(datetime_format, 'dformat')

# 开启debug模式.可以自动刷新
@app.route('/')
def hello_world():  # put application's code here
    return 'Hello World!'

# 在 additional option 添加 --host=0.0.0.0
# 在 additional option 添加 --port=1944
# additional option的选项使用 空格隔开

# http://localhost:2943/blog/1943
@app.route('/blog/<int:blog_id>')
def blog_detail(blog_id):
    return 'Blog {}, {}'.format(blog_id,"2nd param placeholder")


@app.route('/book/list')
def book_list():
    page_no = request.args.get('pageNo',default=1,type=int)
    print(f"page_no: {page_no}")
    page_size = request.args.get('pageSize',default=10,type=int)
    print(f"page_size: {page_size}")
    return f'Book list {page_no} of {page_size}'

@app.route('/template')
def template():
    user = User('john','john@163.com',123456)
    person = {
        "username": "joe",
        "email":"joe@qq.com"
    }
    return  render_template("index.html", user=user, person=person)

@app.route('/blog-detail/<int:blog_id>')
def blog_detail_with_param(blog_id):
    return  render_template("blog_detail.html", blog_id=blog_id)


@app.route("/filter")
def filter_demo():
    my_time = datetime.now()
    user = User("Leslie","leslie@1943.com",123456)
    return  render_template("filter.html", user=user, time=my_time)

@app.route("/control")
def control_statement():
    age = 22
    books = [
        {"id":1, "name":"三國","author":"Luo"},
        {"id": 2, "name": "水滸", "author": "Shi"},
    ]
    return render_template("control.html", age=age, books=books)

@app.route("/child1")
def child1():
    return render_template("child1.html")

@app.route("/static_resource")
def static_resource():
    return render_template("static.html")

if __name__ == '__main__':
    app.run(debug=True)
